/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.p10.miage.gestionabsence.model;

import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author huynal
 */
public class JourBDD {

    public JourBDD() {
    }

    public static Jour trouverParId(int id) throws SQLException, Exception {
        Jour jour = new Jour();

        String sql = "SELECT * FROM JOUR WHERE IDJ = ?";

        PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        pst.setInt(1, id);
        if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            if (resultat.first() == true) {
                jour.init(resultat);
            } else {
                throw new Exception("Jour inconnu");
            }
            resultat.close();
        }
        return jour;
    }

    public static Jour trouverParDate(java.util.Date date) throws SQLException, Exception {
        Jour jour = new Jour();
        java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
        sqlDate = new java.sql.Date(date.getTime());
        String sql = "SELECT * FROM JOUR WHERE date = ? ";
        PreparedStatement pst = Connection.getBDD().prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        pst.setDate(1, sqlDate);
        if (pst.execute()) {
            ResultSet resultat = pst.getResultSet();
            if (resultat.first() == true) {
                jour.init(resultat);
            } else {
                throw new Exception("Jour inconnu");
            }
            resultat.close();
        }
        return jour;
    }
}
